import Vue from 'vue';
import VueRouter, { RouteConfig } from 'vue-router';
import Layout from '@/components/Layout/index.vue';

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: '/',
    name: 'home',
    redirect: '/dashboard',
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () =>
          import(
            /* webpackChunkName: "dashboard" */ '../views/Dashboard/index.vue'
          ),
      },
    ],
  },
  {
    path: '/customer',
    component: Layout,
    children: [
      {
        path: '/customer/list',
        name: '客户管理',
        component: () =>
          import(
            /* webpackChunkName: "CustomerList" */ '../views/CustomerList/index.vue'
          ),
      },
      {
        path: '/customer/detail',
        name: '客户维护',
        component: () =>
          import(
            /* webpackChunkName: "CustomerDetail" */ '@/views/CustomerDetail/index.vue'
          ),
      },
    ],
  },
  {
    path: '/enterprise',
    component: Layout,
    children: [
      {
        path: '/enterprise/list',
        name: '企业客户',
        component: () =>
          import(
            /* webpackChunkName: "EnterpriseList" */ '../views/EnterpriseList/index.vue'
          ),
      },
      {
        path: '/enterprise/detail',
        name: '企业维护',
        component: () =>
          import(
            /* webpackChunkName: "EnterpriseDetail" */ '@/views/EnterpriseDetail/index.vue'
          ),
      },
    ],
  },
  {
    path: '/workflow',
    component: Layout,
    children: [
      {
        path: '/workflow/list',
        name: '工作流',
        component: () =>
          import(
            /* webpackChunkName: "dashboard" */ '../views/WorkflowList/index.vue'
          ),
      },
    ],
  },
  {
    path: '/admin',
    component: Layout,
    children: [
      {
        path: '/admin/user/list',
        name: '系统用户',
        component: () =>
          import(
            /* webpackChunkName: "dashboard" */ '../views/AdminUserList/index.vue'
          ),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () =>
      import(
        /* webpackChunkName: "dashboard" */ '@/views/AdminLogin/index.vue'
      ),
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !sessionStorage.getItem('authorization')) {
    next('/login');
  } else {
    next();
  }
});

export default router;
